<template>
    <div>
        <div style="height: 50px">
            <el-input
                    placeholder="请输入名称"
                    name="courseName"
                    v-model:value="courseName"
                    suffix-icon="el-icon-user"
                    style="width: 200px;padding-right: 5px;"
            >
            </el-input>
            <el-button class="el_margin_lr" type="primary" icon="el-icon-search" @click="search" >搜索</el-button>

            <el-button class="el_margin_lr" type="warning" icon="el-icon-search" @click="reset" >重置</el-button>
        </div>

        <div style="height: 50px">
            <el-button type="primary" @click="title='新增课程',courseForm={},dialogFormVisible=true">新增<i class="el-icon-circle-plus-outline" style="margin-left: 2px"></i></el-button>
            <el-button type="danger" @click="batchDeleteUsers">批量删除<i class="el-icon-remove-outline" style="margin-left: 2px"></i></el-button>

        </div>

        <el-table :data="tableData" header-cell-class-name="headerTableBg" @selection-change="handleSelectionChange">
            <el-table-column
                    type="selection"
                    width="55">
            </el-table-column>
            <el-table-column prop="id" label="ID" width="140"></el-table-column>
            <el-table-column prop="name" label="课程名称" width="180"></el-table-column>
            <el-table-column prop="score" label="学分" ></el-table-column>
            <el-table-column prop="time" label="学时" ></el-table-column>
            <el-table-column  label="是否开课" >
                <template v-slot="{row}">
                    <el-switch
                            v-model="row.state"
                            active-color="#13ce66"
                            inactive-color="#ff4949"
                            @change="changeState(row.id)">
                    </el-switch>
                </template>
            </el-table-column>

            <el-table-column prop="teacherName" label="教师名称" ></el-table-column>

            <el-table-column prop="operation" label="操作">
                <template slot-scope="scope">
                    <el-button type="success" @click="toEditCourse(scope.row)" >编辑<i class="el-icon-edit" style="margin-left: 2px"></i></el-button>

                    <el-popconfirm
                            class="el_margin_lr"
                            confirm-button-text='确定'
                            cancel-button-text='取消'
                            icon="el-icon-info"
                            icon-color="red"
                            title="确认删除该课程吗？"
                            @confirm="deleteCourse(scope.row.id)">
                        <el-button  type="danger" slot="reference">删除<i class="el-icon-remove-outline" style="margin-left: 2px"></i></el-button>
                    </el-popconfirm>
                </template>
            </el-table-column>

        </el-table>
        <div >
            <el-pagination
                    @size-change="handleSizeChange"
                    @current-change="handleCurrentChange"
                    :current-page="pageIndex"
                    :page-sizes="[5, 10, 15, 20]"
                    :page-size="pageSize"
                    layout="total, sizes, prev, pager, next, jumper"
                    :total="totalNum">
            </el-pagination>
        </div>

        <el-dialog :title="title" style="text-align: center;" :visible.sync="dialogFormVisible" width="30%" @close="cancelEdit">
            <div >
                <el-form label-width=80px >
                    <el-form-item label="课程名称" >
                        <el-input v-model="courseForm.name" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="学分" >
                        <el-input v-model="courseForm.score" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="课程时间" >
                        <el-input v-model="courseForm.time" autocomplete="off"></el-input>
                    </el-form-item>
                    <el-form-item label="教师名称" >
                        <el-select v-model="courseForm.teacherId" :placeholder="courseForm.teacherName" style="float: left;width: 100%;">
                            <el-option
                                    v-for="teacher in totalTeachers"
                                    :label="teacher.nickname"
                                    :value="teacher.id">
                            </el-option>
                        </el-select>
                    </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="cancelEdit">取 消</el-button>
                    <el-button type="primary" @click="addCourse">确 定</el-button>
                </div>
            </div>

        </el-dialog>


    </div>
</template>

<script>
    import request from "@/utils/request";
    import {constants} from "@/assets/common/contants";

    export default {
        name: "CourseManager",
        props:{
        },
        data(){
            return{
                tableData:{},
                form:{},
                pageIndex:1,
                totalNum:0,
                pageSize:10,
                multipleSelection:[],
                courseName:"",
                dialogFormVisible:false,
                courseForm:{},
                title:"",
                menuTable:[],
                defaultProps:{
                    children: 'children',
                    label: 'description'
                },
                checkedKeys:[],
                currentCourseId:0,
                totalTeachers:[],
            }
        },
        created() {
            this.load();
            request.get("course/queryAllCoursesNum",{
                params:{
                    courseName: this.courseName
                }
            }).then(res=>{
                if (res.code === '200'){
                    this.totalNum=res.data;
                }
            });
            this.queryAllTeachers();

        },
        methods:{
            queryAllTeachers(){
                request.get("user/queryUser",{
                    params:{
                        roleFlag: constants.ROLE_TEACHER,
                    }
                }).then(res=>{
                    this.totalTeachers=res.usersList;

                })
            },
            load(){
                request.get("course/queryAllCourses",{
                    params:{
                        pageIndex: this.pageIndex,
                        pageSize: this.pageSize,
                        courseName: this.courseName
                    }
                }).then(res=>{
                    if (res.code === '200'){
                        this.tableData=res.data;
                        this.totalNum=res.data.length;

                    }
                })
            },
            toEditCourse(val){
                this.title='修改课程';
                this.dialogFormVisible=true;
                this.courseForm=val;
            },
            cancelEdit(){
                this.dialogFormVisible = false  ;
                this.load();
            },
            addCourse(){
                request.post("course/addCourse",this.courseForm).then(res=>{
                    if (res.code=='200'){
                        this.$message({
                            message: this.title+'成功',
                            type: 'success'
                        });
                        this.dialogFormVisible=false;
                        this.load();
                    }else{
                        this.$message.error(this.title+'失败');
                    }
                })
            },
            search(){
                this.load();
                request.get("course/queryAllCoursesNum",{
                    params:{
                        courseName: this.courseName
                    }
                }).then(res=>{
                    if (res.code === '200'){
                        this.totalNum=res.data;
                    }
                })
            },
            changeState(id){
                request.get("course/editCourseState",{
                    params:{
                        id : id,
                    }
                }).then(res=>{
                    if (res.code === '200'){
                        this.load();
                    }
                })
            },

            handleSelectionChange(val){
                this.multipleSelection=val;
            },
            batchDeleteUsers(){
                let ids=this.multipleSelection.map(val =>val.id);
                request.post("course/batchDeleteCourse",ids).then(res=>{
                    if (res.code === '200'){
                        this.load();
                    }
                })
            },
            handleCurrentChange(val){
                this.pageIndex=val;
                this.load();
            },

            handleSizeChange(val){
                this.pageSize=val;
                this.load();
            },
            deleteCourse(id){
                request.get("course/deleteCourse",{
                    params:{
                        id : id,
                    }
                }).then(res=>{
                    if (res.code === '200'){
                        this.load();
                    }
                })

            },
            reset(){
                this.courseName="";
                this.load();
            }
        }
    }
</script>

<style scoped>

</style>
